/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobiwebinc.compconn.client.processor;

import com.mobiwebinc.compconn.client.*;
import com.mobiwebinc.compconn.communication.IOHandler;
import com.mobiwebinc.compconn.configuration.ClientConfiguration;
import com.mobiwebinc.compconn.events.EventHandler;
import java.util.Vector;

/**
 *
 * @author suraj
 */
public abstract class Processor {

    protected Vector<EventHandler> eventHandlers;
    protected IOHandler io;
    protected ClientConfiguration clientConfiguration;
    ConfigGrabber grabber;

    protected Processor(ConfigGrabber grabber) {
        io = grabber.getIo();
        clientConfiguration = grabber.getClientConfiguration();
        eventHandlers = new Vector<EventHandler>();
        eventHandlers.add(grabber);
        grabber.grabConfig();
    }

    public void process() {
        while (io.isConnected()) {
            String msg = io.readMsg();
            handleRequest(msg);
        }
    }

    protected void handleRequest(String cmd) {
        try {
            for (EventHandler handler : eventHandlers) {
                if (handler.isSupported(cmd)) {
                    handler.handleRequest(cmd);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
